# -*- coding: utf-8 -*-
'''eof
name:申请人报告期内该账户类型（贷款、贷记卡记准贷记卡）累计逾期超过6次
code:IC23_Ind5yearsAccOvduMonth
tableName:
columnName:
groups:行内二代个人征信数据
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:贷款累计逾期次数，贷记卡累计逾期次数，准贷记卡累计逾期次数
eof'''

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def isFloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

#贷款累计逾期月份数
def IC22_Ind5yearsContinuousOvduMonth_1():  
    data = IC_DSJ_2D.get('data')
    factor = []
    # 借贷交易信息概要,出现次数[1..1],下级字段出现次数[0..1]
    PCO = getTable(data, 'PCO', 1, 0)
    if PCO == [{}]:
        return u'缺失值'
    elif PCO == 0:
        return 0
    else:
        # 信贷交易信息概要信息单元,出现次数[0..1],下级字段出现次数[0..1]
        PC02 = getTable(PCO[0], 'PC02', 0, 0)
        if PC02 == [{}]:
            return u'缺失值'
        elif PC02 == 0:
            return 0
        else:
            # 逾期（透支）汇总信息段,出现次数[0..1],下级字段出现次数[1..6]
            PC02D = getTable(PC02[0], 'PC02D', 0, 1)
            if PC02D == [{}]:
                return u'缺失值'
            elif PC02D == 0:
                return 0
            else:
                # 逾期（透支）汇总信息,出现次数[1.6],下级字段出现次数[1..1]
                PC02DH = getTable(PC02D[0], 'PC02DH', 1, 1)
                if PC02DH == [{}]:
                    return u'缺失值'
                elif PC02DH == 0:
                    return 0
                else:
                    if len(PC02DH) > 0:
                        for i in PC02DH:
                            # 账户类型
                            PC02DD01 = i.get("PC02DD01")
                            # 月份数
                            PC02DS03 = i.get("PC02DS03")
                            if PC02DD01 not in null_type_list and PC02DS03 not in null_type_list:
                                #1是非循环贷账户,2是循环额度下分账户,3是循环额度下分账户,4是贷记卡账户,5准贷记卡账户
                                if PC02DD01 in [1,2,3,"1","2","3"]:
                                    if isFloat(PC02DS03):
                                        factor.append(float(PC02DS03))
                                    else:
                                        factor.append(None)
                                else:
                                    pass
                            else:
                                factor.append(None)
                    else:
                        return u'缺失值'
    if None in factor:
        factor = [x for x in factor if x is not None]
        if len(factor) == 0:
            return u'缺失值'
        else:
            return sum(factor)
    elif len(factor) == 0:
        return 0
    else:
        return sum(factor)

#贷记卡累计逾期月份数
def IC22_Ind5yearsContinuousOvduMonth_2():  
    data = IC_DSJ_2D.get('data')
    factor = []
    # 借贷交易信息概要,出现次数[1,1],下级字段出现次数[0,1]
    PCO = getTable(data, 'PCO', 1, 0)
    if PCO == [{}]:
        return u'缺失值'
    elif PCO == 0:
        return 0
    else:
        # 信贷交易信息概要信息单元,出现次数[0..1],下级字段出现次数[0..1]
        PC02 = getTable(PCO[0], 'PC02', 0, 0)
        if PC02 == [{}]:
            return u'缺失值'
        elif PC02 == 0:
            return 0
        else:
            # 逾期（透支）汇总信息段,出现次数[0..1],下级字段出现次数[1..6]
            PC02D = getTable(PC02[0], 'PC02D', 0, 1)
            if PC02D == [{}]:
                return u'缺失值'
            elif PC02D == 0:
                return 0
            else:
                # 逾期（透支）汇总信息,出现次数[1.6],下级字段出现次数[1..1]
                PC02DH = getTable(PC02D[0], 'PC02DH', 1, 1)
                if PC02DH == [{}]:
                    return u'缺失值'
                elif PC02DH == 0:
                    return 0
                else:
                    if len(PC02DH) > 0:
                        for i in PC02DH:
                            # 账户类型
                            PC02DD01 = i.get("PC02DD01")
                            # 月份数
                            PC02DS03 = i.get("PC02DS03")
                            if PC02DD01 not in null_type_list and PC02DS03 not in null_type_list:
                                #1是非循环贷账户,2是循环额度下分账户,3是循环额度下分账户,4是贷记卡账户,5准贷记卡账户
                                if PC02DD01 in [4,"4"]:
                                    if isFloat(PC02DS03):
                                        factor.append(float(PC02DS03))
                                    else:
                                        factor.append(None)
                                else:
                                    pass
                            else:
                                factor.append(None)
                    else:
                        return u'缺失值'
    if None in factor:
        factor = [x for x in factor if x is not None]
        if len(factor) == 0:
            return u'缺失值'
        else:
            return sum(factor)
    elif len(factor) == 0:
        return 0
    else:
        return sum(factor)
    
#准贷记卡累计逾期月份数
def IC22_Ind5yearsContinuousOvduMonth_3():  
    data = IC_DSJ_2D.get('data')
    factor = []
    # 借贷交易信息概要,出现次数[1,1],下级字段出现次数[0,1]
    PCO = getTable(data, 'PCO', 1, 0)
    if PCO == [{}]:
        return u'缺失值'
    elif PCO == 0:
        return 0
    else:
        # 信贷交易信息概要信息单元,出现次数[0..1],下级字段出现次数[0..1]
        PC02 = getTable(PCO[0], 'PC02', 0, 0)
        if PC02 == [{}]:
            return u'缺失值'
        elif PC02 == 0:
            return 0
        else:
            # 逾期（透支）汇总信息段,出现次数[0..1],下级字段出现次数[1..6]
            PC02D = getTable(PC02[0], 'PC02D', 0, 1)
            if PC02D == [{}]:
                return u'缺失值'
            elif PC02D == 0:
                return 0
            else:
                # 逾期（透支）汇总信息,出现次数[1.6],下级字段出现次数[1..1]
                PC02DH = getTable(PC02D[0], 'PC02DH', 1, 1)
                if PC02DH == [{}]:
                    return u'缺失值'
                elif PC02DH == 0:
                    return 0
                else:
                    if len(PC02DH) > 0:
                        for i in PC02DH:
                            # 账户类型
                            PC02DD01 = i.get("PC02DD01")
                            # 月份数
                            PC02DS03 = i.get("PC02DS03")
                            if PC02DD01 not in null_type_list and PC02DS03 not in null_type_list:
                                #1是非循环贷账户,2是循环额度下分账户,3是循环额度下分账户,4是贷记卡账户,5准贷记卡账户
                                if PC02DD01 in [5,"5"]:
                                    if isFloat(PC02DS03):
                                        if float(PC02DS03):
                                            factor.append(float(PC02DS03))
                                        else:
                                            pass
                                    else:
                                        factor.append(None)
                                else:
                                    pass
                            else:
                                factor.append(None)
                    else:
                        return u'缺失值'
    if None in factor:
        factor = [x for x in factor if x is not None]
        if len(factor) == 0:
            return u'缺失值'
        else:
            return sum(factor)
    elif len(factor) == 0:
        return 0
    else:
        return sum(factor)
    
def summary():
    x_result = IC22_Ind5yearsContinuousOvduMonth_1()
    y_result = IC22_Ind5yearsContinuousOvduMonth_2()
    z_result = IC22_Ind5yearsContinuousOvduMonth_3()
    # x = u"贷款累计逾期月份数"
    # y = u"贷记卡累计逾期月份数"
    # z = u"准贷记卡累计逾期月份数"
    # resultDict = {x:x_result,y:y_result,z:z_result}
    if x_result == y_result == z_result == u'缺失值':
        return u'缺失值'
    else:
        result_list = [x_result,y_result,z_result]
        result_list = [x for x in result_list if x != u'缺失值']
        return sum(result_list)

result = summary()
